<template>
  <div class="common-layout">
    <el-container >
      <el-header>
        <el-row >
          <el-col class="colLeft" :span="12">
            <div class="mb-2">能力有限公司</div>
            <div class="mb-3">
              <router-link  :to = "{ path: '/'}" event = "mouseover">
                <el-icon class="Menu" color="#ffffff"  style="margin-top: 15px" :size="30">
                  <Menu />
                </el-icon>
              </router-link>
            </div>
          </el-col>
          <el-col class="colEL" :span="12">
            <el-col class="Avatar">
              <el-dropdown trigger="click">
                  <span class="el-dropdown-link">
                    <div class="el-dropdown-link-div1">
                      <el-avatar @click="visible = true" :size="50" :src="circleUrl" />
                    </div>
                    <div class="el-dropdown-link-div2">
                      <span v-if="store.state.user.sysEmployee && store.state.user.sysEmployee.empName">{{store.state.user.sysEmployee.empName}}</span>
<el-icon class="el-icon--right"><arrow-down /></el-icon>
                    </div>
                  </span>
                <template #dropdown>
                  <el-dropdown-menu>
                    <div class="Personal_Div_Information">
                      <div class="Personal_Information">
                        <div class="Personal_Img">
                          <el-avatar @click="visible = true" :size="40" :src="circleUrl" />
                        </div>
                        <div class="Personal_Dept">
                            <span class="Personal_Name">
                              <span v-if="store.state.user.sysEmployee && store.state.user.sysEmployee.empName">
                                {{store.state.user.sysEmployee.empName}}</span>
                            </span>
                          <span class="Personal_Position">
                            <span v-if="store.state.user.sysEmployee && store.state.user.sysEmployee.employeePost">
                              {{store.state.user.sysEmployee.employeePost.post.postName}}</span>
                            </span>
                          <br>
                          <div>
                            能力有限公司/<span v-if="store.state.user.sysEmployee && store.state.user.sysEmployee.dept">{{store.state.user.sysEmployee.dept.deptName}}</span>
                          </div>
                        </div>
                      </div>
                      <div class="Underline">
                      </div>
                    </div>
                    <div class="Personal_Sign_Out">
                      <el-dropdown-item :disabled="shangbutton" @click="shangqian">
                        <div event = "mouseover">
                          <el-icon><SwitchButton /></el-icon>
                          上班打卡
                        </div>
                      </el-dropdown-item>
                      <el-dropdown-item :disabled="xx" @click="downqian">
                        <div event = "mouseover">
                          <el-icon><SwitchButton /></el-icon>
                          下班打卡
                        </div>
                      </el-dropdown-item>
                      <el-dropdown-item>
                        <router-link :to="{ path:'/'}" event = "mouseover">
                          <el-icon><User /></el-icon>
                          个人中心
                        </router-link>
                      </el-dropdown-item>
                      <el-dropdown-item @click="lout">
                        <router-link  :to="{ path:'/login'}" event = "mouseover">
                          <el-icon><SwitchButton /></el-icon>
                          退出
                        </router-link>
                      </el-dropdown-item>
                    </div>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </el-col>
          </el-col>
        </el-row>
      </el-header>
      <el-container >
        <el-aside width="200px">
          <el-scrollbar height="680px">
            <el-row>
              <el-col>
                <el-menu
                    default-active="0"
                    :router="true"
                    active-text-color="#fFfFfFCC"
                    background-color="#2a2e35"
                    class="el-menu-vertical-demo"
                    text-color="#fff"

                >
                  <TreeItem2 :list="menuList"></TreeItem2>
                </el-menu>

              </el-col>
            </el-row>
          </el-scrollbar>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup lang="ts" >
import {reactive, toRefs, ref, onBeforeMount} from "vue";
import {useStore} from "vuex";
import axios from "axios";
import TreeItem from "./capacity/cjl/TreeItem.vue";
import {useRouter} from "vue-router";
import moment from "moment";
import TreeItem2 from "./capacity/cjl/treeItem2.vue"
import {ElMessage, ElMessageBox} from "element-plus";

//注册局部组件
components:{
  TreeItem:TreeItem
  TreeItem2:TreeItem2
}
//菜单数据
const menuList=ref()
const visible = ref(false)
const tt = new Date();
const shangqian=()=> {
  if(8<=tt.getHours() && tt.getHours()<12) {
    if(aa.value==2 || aa.value==4){
      ElMessage({
        message: "今天上班卡已打哦！如有疑惑请联系考勤管理员！",
        type: 'warning',
      })
    }else{
      ElMessageBox.confirm(
          '打卡完毕上班卡不在开放，请确认？',
          {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
          }
      ).then(() => {
        let str=ref(moment(tt).format('yyyy-MM-DD HH:mm:ss'))
        axios.post("checkOa/attendance/add/"+str.value+"/"+1).then((res)=>{
          isCar();
          console.log(res.data.data)
          let i=res.data.data
          if(i==1){
            ElMessage({
              type: 'warning',
              message: '今天您已请假，不需要打卡！',
            })
          }
          if(i==3){
            ElMessage({
              message: "今天不是出勤日！",
              type: 'warning',
            })
          }
          if(i==4){
            ElMessage({
              message: "今天加班日，不需要打卡！",
              type: 'warning',
            })
          }
          if(i==5){
            ElMessage({
              message: "今天出差日，不需要打卡！",
              type: 'warning',
            })
          }
          if(i==2){
            ElMessage({
              type: 'success',
              message: '您在'+str.value+'打卡成功！',
            })
          }
        })
      }).catch(() => {

      })
    }
  }else{
    ElMessage({
      message: "请在规定时间内打卡！如有疑惑请联系考勤管理员！",
      type: 'warning',
    })
  }
}

const downqian=()=>{
  console.log(tt.getHours())
  if(12<tt.getHours() && tt.getHours() <20){
    if(aa.value==1 || aa.value==4){
      ElMessage({
        message: "今天下班卡已打哦！如有疑惑请联系考勤管理员！",
        type: 'warning',
      })
    }else{
      ElMessageBox.confirm(
          '打卡完毕下班卡不在开放，请确认？',
          {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
          }
      ).then(() => {
        const tt = new Date();
        let str=ref(moment(tt).format('yyyy-MM-DD HH:mm:ss'))
        axios.post("checkOa/attendance/add/"+str.value+"/"+2).then((res)=>{
          isCar();
          console.log(res.data.data)
          let i=res.data.data
          if(i==1){
            ElMessage({
              type: 'warning',
              message: '今天您已请假，不需要打卡！',
            })
          }
          if(i==3){
            ElMessage({
              message: "今天不是出勤日！",
              type: 'warning',
            })
          }
          if(i==4){
            ElMessage({
              message: "今天加班日，不需要打卡！",
              type: 'warning',
            })
          }
          if(i==5){
            ElMessage({
              message: "今天出差日，不需要打卡！",
              type: 'warning',
            })
          }
          if(i==2){
            ElMessage({
              type: 'success',
              message: '您在'+str.value+'打卡成功！',
            })
          }
        })
      }).catch(() => {

      })
    }
  }else{
    ElMessage({
      message: "请在规定时间内打卡！如有疑惑请联系考勤管理员！",
      type: 'warning',
    })
  }
}

const store =useStore()

const circleUrl = ref("http://e8.weaver.com.cn/messager/usericon/c82011e5326e411da4bc3a137b95e767.jpg")
const router=useRouter()
const lout=()=>{
    store.commit("cleanInfo")
    localStorage.clear();
    router.push("/login")
}
const shangbutton=ref(true);//上班打卡禁用
const xx=ref(true);//下班打卡禁用
const aa=ref(0)
const isCar=()=>{
  const str=ref(moment(new Date).format('yyyy-MM-DD'))
  axios.post("checkOa/attendance/select1/"+str.value).then((res)=>{
    aa.value=res.data.data
    if(8<=tt.getHours() && tt.getHours() <12) {
      console.log("早上"+tt)
      shangbutton.value=false
      xx.value=true
    }
    if(12<=tt.getHours() && tt.getHours() <20){
      console.log("晚上")
      shangbutton.value=true
      xx.value=false
    }
    //全部已经打卡
    if(aa.value==4){
      shangbutton.value=true
      xx.value=true
    }
    if(aa.value==2){
      shangbutton.value=true
    }
    //下班卡打卡
    if(aa.value==1){
      xx.value=true
    }
  })
}
onBeforeMount(function (){
  //左侧菜单
  axios.post("adminOa/menu/getMenu").then((res)=>{
    menuList.value=res.data.data
  })
  //是否已经打卡
  isCar();
})
</script>

<style lang="scss" scoped>

.colLeft{
  display: flex;
  flex-direction: row;
}
.mb-3{
  margin-left: 20px;
}
.common-layout{
  height: 100%;
  width: 100%;
}
.el-container {
  height: 100%;
}
.el-aside{
  background-color: #2a2e35;
  /*#636c7c*/
  color: #FFFFFFCC;
}
.mb-2{
  text-align: center;
  line-height: 60px;
  color:#ffffff;
  font-size: 14px;
  font-family: "微软雅黑";
  background-color: #008df7;
  margin: 0px;
  height: 60px;
  width: 200px;
}
.el-menu{
  border: 0px solid;
}
.el-menu-item{
  background-color: #2a2e35;
  /*#636c7c*/
  color: #FFFFFFCC;
}
.el-sub-menu{
  --bg-color:#2a2e35;
  background-color: #2a2e35;
  color: #FFFFFFCC;
}
.is-opened{
  background-color: #2a2e35;
  color: #FFFFFFCC;
}
.is-active{
  background-color: #636c7c;
  color: #FFFFFFCC;
}
.el-sub-menu__title span{
  background-color: #2a2e35;
  color: #FFFFFFCC;
}
.el-header{
  background-color: #0270c1;
  padding: 0px 20px 0px 0px;
  color:#fff;
}
.Menu:hover{
  cursor: pointer;
}
.el-row{
  height: 100%;
}
.el-col{
  height: 100%;

}
.Avatar:hover{
  cursor: pointer;
}
.Avatar{
  width: 150px;
  float: right;

}
.el-dropdown{
  //float: right;
  line-height: 60px;
  width: 100%;
  height: 60px;
}
.el-dropdown-link{
  width: 100%;
  text-align: right;
  color: #ffffff;
  display: flex;

}
.el-dropdown-link span{
  margin-top: 5px;

}
.el-dropdown-link-div2{
  float: right;
  margin-left: 20px;
}
.Personal_Div_Information{
  width: 220px;
  height: 65px;
}
.Personal_Information{
  display: flex;
  align-items: center;
}
.Personal_Img{
  padding: 7px;
  width: 40px;
  height: 40px;
}
.Underline{
  width: 100%;
  height: 1px;
  margin: 5px 0;
  background: #e9e9e9;
}
.Personal_Dept{
  width: 150px;
  margin: 0px 0px 0px 10px;
  height: 54px;
  line-height: 27px;
  color: #909090;
}
.Personal_Position{
  margin: 0px 0px 0px 10px;
}
.Personal_Name{
  color: #4D7AD8;
}
.el-dropdown-menu__item a {
  text-decoration: none;
  color: #656B72;
  width: 100%;
}
.el-dropdown-menu__item a:hover{
  text-decoration: none;
  color: #656B72;

}
.Personal_Sign_Out li:hover{
  background-color: #EAEAEA;
}
.el-scrollbar__view{
  height: 100%;
}

</style>